{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 模型构造\n",
    "## 自定义层"
   ],
   "id": "4d3c465409214e09"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 不含参数的层",
   "id": "59ff87d533ec9c5d"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "首先定义一个不含任何参数的自定义层",
   "id": "69c9048af882739a"
  },
  {
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-16T12:22:10.231983Z",
     "start_time": "2025-08-16T12:22:08.651198Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as f"
   ],
   "id": "5e8328f3cdc40bf",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T12:22:12.470448Z",
     "start_time": "2025-08-16T12:22:12.467446Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class CenteredLayer(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "    def forward(self,X):\n",
    "        return X - X.mean()"
   ],
   "id": "2e88833546c06460",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T12:22:37.630752Z",
     "start_time": "2025-08-16T12:22:37.621221Z"
    }
   },
   "cell_type": "code",
   "source": [
    "layer = CenteredLayer()\n",
    "layer(torch.FloatTensor([1,2,3,4,5]))"
   ],
   "id": "8a0cd6a645d1972f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-2., -1.,  0.,  1.,  2.])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:38:48.351360Z",
     "start_time": "2025-08-16T13:38:48.345988Z"
    }
   },
   "cell_type": "code",
   "source": "net = nn.Sequential(nn.Linear(8,128),CenteredLayer())",
   "id": "af844fcba2a87923",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:40:27.040314Z",
     "start_time": "2025-08-16T13:40:27.032253Z"
    }
   },
   "cell_type": "code",
   "source": [
    "Y = net(torch.rand(4,8))\n",
    "print('Y:',Y,'\\nY mean:',Y.mean())"
   ],
   "id": "bf1a4166ad80deca",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Y: tensor([[ 1.3540e-01, -1.4863e-01, -2.7012e-01, -2.8600e-01, -5.0466e-01,\n",
      "         -6.7926e-01,  5.0883e-02,  2.0740e-01,  3.4444e-01,  7.9898e-01,\n",
      "          3.9307e-01,  5.8672e-01,  3.8199e-01, -5.4017e-01,  6.0830e-01,\n",
      "         -2.7347e-01, -3.3985e-01, -8.9357e-01, -1.8102e-01,  9.4595e-01,\n",
      "         -1.8667e-01, -5.8705e-02, -5.9046e-01, -1.8924e-01, -1.9349e-02,\n",
      "         -4.2041e-01,  5.4339e-01, -4.7553e-01,  6.2979e-01, -2.2983e-01,\n",
      "          4.5050e-01, -4.0861e-01,  3.4752e-01,  6.6953e-01,  1.5301e-01,\n",
      "         -3.8692e-01, -6.1887e-01, -5.3258e-01, -2.6687e-01,  3.0830e-01,\n",
      "         -1.6006e-01, -2.0743e-01,  1.8179e-01,  2.1973e-01, -5.0060e-01,\n",
      "          3.1520e-01, -3.6322e-01, -2.5320e-01, -1.6941e-01,  3.6034e-02,\n",
      "          2.0043e-01, -7.5096e-02, -5.7889e-02, -5.1829e-01, -6.3602e-02,\n",
      "         -1.0410e+00, -4.0768e-01,  3.0124e-02,  1.6677e-01, -3.4043e-02,\n",
      "         -2.8753e-01,  3.2670e-01, -7.5614e-02,  2.8569e-01,  2.4699e-02,\n",
      "         -4.3799e-01,  1.6816e-01, -4.0598e-01, -4.5198e-01,  1.1227e-02,\n",
      "         -2.2883e-01,  6.2202e-01,  3.6168e-01, -3.6264e-01,  1.5087e-01,\n",
      "          2.0380e-03, -3.6762e-01,  6.0463e-01, -1.1789e-01,  3.3996e-01,\n",
      "          2.1838e-01,  3.4427e-01, -3.9056e-02, -3.8321e-01,  2.6792e-01,\n",
      "          1.3960e-01,  6.4591e-01,  2.2919e-01,  4.6530e-01,  6.2659e-01,\n",
      "         -1.4572e-01,  5.5957e-01, -1.6976e-01,  4.1952e-01,  4.9123e-01,\n",
      "          3.3638e-01,  3.0918e-01,  5.4380e-01,  4.6588e-01,  9.2805e-03,\n",
      "          5.7136e-01, -4.1877e-01,  6.1923e-01, -1.8646e-01, -3.0061e-01,\n",
      "          1.9100e-01,  7.1317e-02,  1.8581e-01,  2.2567e-01, -6.5422e-01,\n",
      "          7.5555e-02, -1.8246e-01, -1.1462e-01, -4.1558e-01, -1.0122e-02,\n",
      "         -6.2275e-02, -4.5846e-01, -1.3005e-01, -4.3457e-01, -2.8918e-01,\n",
      "         -2.5481e-01, -2.9032e-01,  4.9797e-01,  3.8516e-01,  5.9192e-02,\n",
      "          9.4541e-02, -5.6356e-01,  5.9710e-01],\n",
      "        [ 1.3280e-01, -1.5796e-01, -5.7876e-01, -3.7960e-01, -2.4427e-01,\n",
      "         -4.8769e-01,  4.4753e-01,  5.1254e-01,  8.0037e-02,  7.9757e-01,\n",
      "          2.3108e-01,  4.8233e-01,  6.1260e-01, -3.5663e-01,  1.8608e-01,\n",
      "         -5.0365e-01, -1.0709e-01, -4.1129e-01, -1.5126e-01,  9.6957e-01,\n",
      "         -1.4678e-02,  2.9200e-01, -7.1557e-01,  1.0152e-01, -3.2599e-01,\n",
      "          2.8271e-04,  1.9083e-01, -1.6805e-01,  7.7611e-01,  2.5796e-01,\n",
      "          5.8734e-01, -6.7762e-01,  4.1292e-01,  7.4573e-01,  1.5755e-01,\n",
      "         -4.6806e-01, -6.6630e-01, -1.3208e-01, -8.5552e-02,  2.0721e-01,\n",
      "          7.5672e-02,  7.9465e-02,  2.8200e-01,  1.7611e-01, -2.9733e-01,\n",
      "         -1.4742e-01, -3.5062e-02, -2.2678e-01, -9.1139e-02,  4.4265e-01,\n",
      "          6.4569e-01,  2.8817e-02,  8.1612e-02, -6.9943e-01,  3.7692e-01,\n",
      "         -9.5720e-01, -4.4738e-01,  1.2234e-01,  4.2491e-01,  1.2175e-01,\n",
      "         -3.5656e-01,  1.1511e-01, -4.1662e-01,  5.5178e-01,  7.2248e-02,\n",
      "         -3.3821e-01,  5.0973e-01, -4.6922e-01, -3.9954e-01, -1.6769e-01,\n",
      "         -3.2644e-02,  4.8367e-01,  4.7649e-01, -1.4711e-01, -2.3046e-01,\n",
      "         -2.5282e-01, -1.0398e-01,  5.4667e-01, -4.6236e-01,  4.5473e-01,\n",
      "          2.3332e-01,  3.7530e-01,  1.2595e-01, -5.1092e-01,  4.2122e-02,\n",
      "         -5.5323e-02,  2.9889e-01,  1.7386e-01,  8.0705e-01,  2.8342e-01,\n",
      "         -3.1818e-01,  2.5621e-01, -4.7295e-01,  2.2980e-01,  4.1941e-01,\n",
      "          2.4475e-01,  2.9785e-01,  7.8860e-01,  5.1774e-01, -9.4207e-02,\n",
      "          6.1084e-01, -3.6317e-01,  4.3068e-01, -1.7313e-01, -2.1686e-01,\n",
      "         -3.6183e-01,  2.5897e-01,  1.7256e-01,  2.0726e-01, -4.9828e-01,\n",
      "         -3.9814e-01, -6.3333e-02, -1.6400e-01, -6.1264e-01, -3.4500e-02,\n",
      "         -2.4651e-01, -3.1375e-01, -2.9945e-01,  5.1684e-02, -3.9941e-01,\n",
      "         -1.0495e-01, -2.0940e-01,  2.6657e-01,  7.3117e-01, -1.1549e-01,\n",
      "          1.2460e-02, -9.2905e-01,  2.7216e-01],\n",
      "        [ 1.3148e-01, -3.1676e-01, -5.0813e-01, -2.9487e-01, -1.3656e-01,\n",
      "         -2.1884e-01,  2.2145e-01,  5.5999e-02, -1.5444e-01,  5.5003e-01,\n",
      "          4.1903e-01,  4.4088e-01,  4.7097e-01, -1.3323e-01, -1.5962e-01,\n",
      "         -5.3948e-01, -1.1775e-01, -2.8511e-01, -2.2028e-01,  6.2749e-01,\n",
      "          2.7894e-01,  1.8260e-01, -4.6911e-01,  3.4362e-01, -7.2566e-02,\n",
      "         -2.3190e-01, -9.2954e-02, -2.0914e-02,  5.2292e-01, -4.7409e-02,\n",
      "          4.6169e-01, -4.9894e-01,  5.2542e-01,  3.2300e-01,  1.6517e-01,\n",
      "         -8.1757e-01, -3.6822e-01, -2.4838e-01, -2.5824e-01, -3.1717e-05,\n",
      "         -1.7270e-02, -3.1288e-02,  4.1945e-01,  1.1507e-01, -4.1819e-02,\n",
      "         -2.7602e-01,  9.8413e-03, -3.5784e-01,  3.7495e-02,  2.3481e-01,\n",
      "          4.9047e-01,  3.3393e-01,  1.0908e-01, -6.6244e-01,  2.9219e-01,\n",
      "         -6.7755e-01, -6.5427e-02,  9.3904e-02,  1.7415e-01,  1.3480e-01,\n",
      "         -1.3551e-01,  1.2432e-01, -3.5367e-01,  4.5969e-01, -2.0568e-01,\n",
      "         -4.4120e-01,  2.5115e-01, -5.9906e-01, -2.0776e-01, -1.8047e-01,\n",
      "         -2.9383e-02,  3.3369e-01,  2.2300e-01,  4.8048e-02, -2.8312e-01,\n",
      "         -1.9237e-01,  7.1691e-02,  4.4827e-01, -3.8909e-01,  1.3353e-01,\n",
      "         -6.1004e-02,  1.9333e-01, -1.3238e-02, -4.3875e-01, -3.3419e-02,\n",
      "          1.6001e-01, -1.2504e-02, -5.3405e-02,  4.0492e-01, -1.4436e-01,\n",
      "         -3.8371e-01,  1.7071e-01, -3.3123e-01, -5.1801e-02,  4.0650e-01,\n",
      "          1.3209e-01,  3.2557e-01,  4.0566e-01,  6.1301e-01, -2.6332e-01,\n",
      "          3.9940e-01, -3.1369e-01,  3.1495e-01, -4.9930e-01, -3.0760e-01,\n",
      "         -2.2610e-01,  2.6087e-01, -9.2362e-02, -9.9217e-03, -4.2195e-01,\n",
      "         -4.8206e-01,  4.5003e-02,  5.0187e-03, -4.5130e-01, -9.6361e-02,\n",
      "         -1.2958e-01, -1.5761e-01, -3.8517e-01,  2.5320e-01, -2.2909e-01,\n",
      "         -8.5058e-02,  2.9410e-03,  3.6398e-01,  6.1879e-01, -6.2763e-03,\n",
      "          2.8208e-01, -7.7178e-01,  1.7744e-01],\n",
      "        [ 1.4040e-01, -1.3547e-01, -3.7561e-01, -2.2854e-01, -4.9077e-01,\n",
      "         -6.4295e-01,  3.2754e-03, -2.4211e-01,  2.9687e-02,  4.8453e-01,\n",
      "          6.6331e-01,  6.0665e-01,  6.5608e-01, -4.0790e-01,  4.7149e-02,\n",
      "         -5.0021e-01, -4.7088e-01, -6.2911e-01, -4.2159e-02,  8.2841e-01,\n",
      "          1.4375e-02,  1.2462e-02, -2.3514e-01,  8.2553e-02,  1.4572e-01,\n",
      "         -4.9068e-01,  2.9525e-01, -1.5058e-01,  6.4536e-01, -1.4344e-01,\n",
      "          2.1679e-01, -4.2071e-01,  4.6453e-01,  3.1653e-01,  9.2933e-02,\n",
      "         -6.9243e-01, -3.9839e-01, -5.2470e-01, -3.1801e-01,  2.6663e-01,\n",
      "          6.3121e-02, -4.2859e-01,  2.9442e-01,  3.1163e-01, -2.7496e-01,\n",
      "         -2.5798e-01, -6.8028e-02, -2.6575e-01, -2.0373e-01,  1.5669e-01,\n",
      "          2.8474e-01,  3.0469e-01, -1.2711e-01, -8.2290e-01, -4.2106e-02,\n",
      "         -9.2219e-01, -2.3940e-01, -3.9014e-02, -2.0845e-01, -3.4015e-02,\n",
      "         -1.6548e-01,  1.6873e-01, -1.0180e-01,  5.3477e-01, -4.4511e-01,\n",
      "         -3.2781e-01,  1.3806e-01, -3.6361e-01, -1.7237e-01,  8.5927e-02,\n",
      "         -2.9003e-01,  5.4480e-01,  1.8640e-01,  3.4345e-02,  6.8064e-02,\n",
      "         -1.3276e-01, -1.3255e-01,  5.7342e-01, -2.2545e-01,  2.7426e-01,\n",
      "          3.1157e-01,  4.8292e-02, -2.0457e-01, -1.9051e-01,  2.8677e-01,\n",
      "          2.2797e-01,  1.0602e-02,  7.4229e-02,  5.6335e-01,  1.5649e-01,\n",
      "         -4.7244e-01,  5.4204e-01, -3.1452e-01,  1.6906e-01,  5.2949e-01,\n",
      "          3.5116e-01,  2.0549e-01,  4.2189e-01,  8.6810e-01, -4.7406e-02,\n",
      "          2.7323e-01, -3.2577e-01,  5.0224e-01, -2.2872e-01, -1.9788e-01,\n",
      "         -2.7790e-03,  4.8922e-02,  2.8596e-01,  1.0444e-01, -3.0516e-01,\n",
      "         -4.4954e-01,  8.9894e-02,  2.2643e-01, -6.4855e-01, -1.6378e-01,\n",
      "         -2.1034e-01, -3.8910e-01, -3.3850e-01,  6.9485e-02,  4.3454e-02,\n",
      "         -3.2356e-01, -2.5673e-02,  4.3891e-01,  5.5777e-01,  1.7215e-01,\n",
      "          1.4076e-01, -5.3619e-01,  3.1592e-01]], grad_fn=<SubBackward0>) \n",
      "Y mean: tensor(-4.8894e-09, grad_fn=<MeanBackward0>)\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": " ### 含参数的层",
   "id": "c25abf1e8864e670"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:44:55.665690Z",
     "start_time": "2025-08-16T13:44:55.661688Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class DIYLinear(nn.Module):\n",
    "    def __init__(self,in_units,units):\n",
    "        super().__init__()\n",
    "        self.weight = nn.Parameter(torch.randn(in_units,units))\n",
    "        self.bias = nn.Parameter(torch.randn(units,))\n",
    "    def forward(self,X):\n",
    "        linear = torch.matmul(X,self.weight.data) + self.bias.data\n",
    "        return f.relu(linear)"
   ],
   "id": "ce8c7fcecdc9aef9",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:45:18.531262Z",
     "start_time": "2025-08-16T13:45:18.523783Z"
    }
   },
   "cell_type": "code",
   "source": [
    "linear = DIYLinear(5,4)\n",
    "linear.weight"
   ],
   "id": "fea40d23347e3002",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Parameter containing:\n",
       "tensor([[-4.9879e-01,  6.8863e-02,  9.1158e-02,  3.7453e-02],\n",
       "        [ 1.1778e+00, -2.6856e-01,  1.5042e-04,  7.4874e-01],\n",
       "        [ 1.6500e-01, -2.6676e-01, -1.8133e-01, -2.8507e-01],\n",
       "        [ 6.8011e-01,  3.4083e-01,  1.1676e-01,  6.7096e-01],\n",
       "        [ 9.5688e-01,  9.5448e-01, -1.0315e-01,  5.0840e-01]],\n",
       "       requires_grad=True)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:45:59.100468Z",
     "start_time": "2025-08-16T13:45:59.082494Z"
    }
   },
   "cell_type": "code",
   "source": "linear(torch.rand(2,5))",
   "id": "bf7c4ca39e66954d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1.1860, 2.1124, 1.7856, 0.2497],\n",
       "        [1.9866, 1.9440, 1.6813, 0.5057]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:47:03.521490Z",
     "start_time": "2025-08-16T13:47:03.517384Z"
    }
   },
   "cell_type": "code",
   "source": [
    "net = nn.Sequential(DIYLinear(64,8),DIYLinear(8,1))\n",
    "print(torch.rand(2,64))"
   ],
   "id": "b694aeba95e37a8f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.3137, 0.9684, 0.8956, 0.4057, 0.9932, 0.9068, 0.7084, 0.5143, 0.0626,\n",
      "         0.0854, 0.2087, 0.4538, 0.5097, 0.9426, 0.7375, 0.4215, 0.0415, 0.1286,\n",
      "         0.8752, 0.6672, 0.0100, 0.2074, 0.6969, 0.8434, 0.3792, 0.7404, 0.8191,\n",
      "         0.9546, 0.3119, 0.0651, 0.9767, 0.6881, 0.4359, 0.8608, 0.2101, 0.8801,\n",
      "         0.7215, 0.2735, 0.9983, 0.1754, 0.1875, 0.0829, 0.3481, 0.9724, 0.1636,\n",
      "         0.7373, 0.7877, 0.0650, 0.9504, 0.4468, 0.2929, 0.4976, 0.9750, 0.9033,\n",
      "         0.9797, 0.9342, 0.4029, 0.5267, 0.7599, 0.0855, 0.9859, 0.1663, 0.7756,\n",
      "         0.7130],\n",
      "        [0.7687, 0.9556, 0.2782, 0.3545, 0.0434, 0.1248, 0.3841, 0.8943, 0.7414,\n",
      "         0.9172, 0.3520, 0.4012, 0.1298, 0.2744, 0.5231, 0.1833, 0.4913, 0.0857,\n",
      "         0.4414, 0.1114, 0.4046, 0.7604, 0.5081, 0.1511, 0.8466, 0.4513, 0.8113,\n",
      "         0.5830, 0.2346, 0.3992, 0.1309, 0.1134, 0.3294, 0.7288, 0.2579, 0.3668,\n",
      "         0.5199, 0.9101, 0.7921, 0.9461, 0.6266, 0.7770, 0.8864, 0.5935, 0.9643,\n",
      "         0.0526, 0.7385, 0.6087, 0.1296, 0.2986, 0.4871, 0.8520, 0.1329, 0.3216,\n",
      "         0.9935, 0.2657, 0.6944, 0.8706, 0.6182, 0.3117, 0.9376, 0.7889, 0.2286,\n",
      "         0.6375]])\n"
     ]
    }
   ],
   "execution_count": 13
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
